<template>
  <div class="common-layout">
    <el-container>
      <el-header class="header">
        <div class="logo">博客后台管理</div>
        <div id="user-info">
          <p>{{ adminStore.adminInfo.AdminName }}, 欢迎你</p>
          <el-button @click="handleLogout" type="primary" class="logout-btn">退出</el-button>
        </div>
      </el-header>
      <el-container class="home-container">
        <el-aside width="200px">
          <el-menu 
            active-text-color="#ffd04b" 
            router 
            background-color="#2d3a4b" 
            class="el-menu-vertical-demo" 
            default-active="/home/student" 
            text-color="#fff" 
            @open="handleOpen" 
            @close="handleClose"
          >
            <el-menu-item index="/admin/home/user">
              <el-icon><Avatar /></el-icon>
              <span>用户管理</span>
            </el-menu-item>
            <el-menu-item index="/admin/home/boke">
              <el-icon><Document /></el-icon>
              <span>博客管理</span>
            </el-menu-item>
            <el-menu-item index="/admin/home/comment">
              <el-icon><Comment /></el-icon>
              <span>评论管理</span>
            </el-menu-item>
          </el-menu>
        </el-aside>
        <el-main>
          <!-- 嵌套路由 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { Document, Avatar, Comment} from '@element-plus/icons-vue'
import { useAdminStore } from '../store/useAdminstore';
import { useRouter } from 'vue-router'

const router = useRouter()
const adminStore = useAdminStore()

console.log('仓库里的数据：')
console.log(adminStore.adminInfo)

const handleOpen = (key, keyPath) => {
  console.log(key, keyPath)
}

const handleClose = (key, keyPath) => {
  console.log(key, keyPath)
}

const handleLogout = async () => {
  try {
    await router.push('/admin/login')
  } catch (error) {
    console.error('Logout failed:', error)
  }
}
</script>

<style scoped>
/* style里面有scoped这个属性则代表这里的css只会对当前组件有效 */
.el-container {
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
.home-container {
  height: calc(100vh - 60px);
}
.home-container > .el-aside,
.home-container > .el-main {
  height: 100%;
}
.el-menu {
  height: 100%;
  border-right: none;
}
.header {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #2d3a4b;
  color: #fff;
  font-size: 18px;
  font-weight: bold;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
#user-info {
  position: absolute;
  right: 20px;
  display: flex;
  align-items: center;
}
#user-info p {
  margin-right: 20px;
  color: #fff;
  font-size: 14px;
}
.logout-btn {
  background-color: #ff4d4f;
  border-color: #ff4d4f;
  color: #fff;
  font-size: 14px;
  padding: 8px 16px;
  border-radius: 4px;
  transition: background-color 0.3s ease;
}
.logout-btn:hover {
  background-color: #ff7875;
  border-color: #ff7875;
}
</style>